package com.action;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bean.ResponseBean;
import com.pojo.MobanFm;
import com.service.MobanFmService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/mobanFm")
public class MobanFmAction {

    @Autowired
    private MobanFmService mobanFmService;

    @Value("${upload.dir}")
    private String upload;

    @Value("${upload.tmp.dir}")
    private String tmp;

    /**
     * 新增模板反面
     * @param mobanFm
     * @return
     */
    @RequestMapping("/addMobanFm")
    public ResponseBean addMobanFm(@RequestBody MobanFm mobanFm){
        if(StringUtil.isNull(mobanFm.getBg())){
            return new ResponseBean(3,1,"模板背景不能为空");
        }
        long i = mobanFmService.nameCount(mobanFm.getName());
        if(i>0){
            return new ResponseBean(2,1,"该模板名称已存在！请重新输入");
        }
        if(mobanFmService.save(mobanFm)){
            FileUtil.copy(tmp + mobanFm.getBg(), upload, true) ;
            return new ResponseBean(0,1,"新增成功");
        }
        return new ResponseBean(1,1,"新增失败");
    }

    /**
     * 修改模板反面
     * @param mobanFm
     * @return
     */
    @RequestMapping("/updMobanFm")
    public ResponseBean updMobanFm(@RequestBody MobanFm mobanFm){

        if(StringUtil.isNull(mobanFm.getBg())){
            return new ResponseBean(3,1,"模板背景不能为空");
        }
        long x = mobanFmService.nameCount(mobanFm.getName());
        long y = mobanFmService.nameIdCount(mobanFm.getId(),mobanFm.getName());
        if(x>=1 && y==0){
            return new ResponseBean(2,1,"该模板名称已存在！请重新输入");
        }
        MobanFm moban = mobanFmService.selectID(mobanFm.getId());
        if(mobanFmService.updateById(mobanFm)){
            if(StringUtil.isNotNull(moban.getBg()) && mobanFm.getBg().equals(moban.getBg())==false){
                FileUtil.copy(tmp + mobanFm.getBg(), upload, true) ;
            }
            return new ResponseBean(0,1,"修改成功");
        }
        return new ResponseBean(1,1,"修改失败");
    }

    /**
     * 查找所有反面模板
     * @return
     */
    @RequestMapping("/selectMobanFm")
    public ResponseBean selectMobanFm(int page, int limit, String name){
        QueryWrapper query = new QueryWrapper() ;
        if(StringUtil.isNotNull(name)){
            query.like("name",name);
        }
        Page p = new Page(page,limit);
        IPage<MobanFm> iPage = mobanFmService.page(p,query);
        return new ResponseBean(0,iPage.getTotal(),"查找成功",iPage.getRecords());
    }

    /**
     * 根据ID查询反面模板
     * @return
     */
    @RequestMapping("/selectMobanFms")
    public ResponseBean selectMobanFm(String id){
        List<MobanFm> list = mobanFmService.list(new QueryWrapper<MobanFm>().eq("status" , 1));
        return new ResponseBean(0,"",list);
    }

    /**
     * 修改反面模板状态
     * @param id        模板ID
     * @param status    模板状态
     * @return
     */
    @RequestMapping("/updMobanFmStatus")
    public ResponseBean updMobanStatus(String id, int status){
        MobanFm mobanFm = new MobanFm();
        mobanFm.setId(id);
        mobanFm.setStatus(status);
        if(mobanFmService.updateById(mobanFm)){
            return new ResponseBean(0,1,"修改状态成功");
        }
        return new ResponseBean(1,1,"修改状态失败");
    }
}
